/*
 * VerDetalleRemito.java
 *
 * Created on 27 de marzo de 2007, 19:45
 */

package vista;

import controlador.DetalleRemito;
import controlador.Remito;
import java.awt.Color;
import java.util.LinkedList;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author  PC
 */
public class VerDetalleRemito extends javax.swing.JFrame {
    
    private Remito remito;
    /** Creates new form VerDetalleRemito */
    public VerDetalleRemito(Remito rem) {
        this.remito = rem;
        initTabla();
        initComponents();
        numero.setText(remito.getNumero());
        fecha.setText(remito.getFecha());
        cliente.setText(remito.getCliente().getNombre());
        if(remito.isCompleto()){
            completo.setText("Este remito ha sido pagado en su TOTALIDAD");
            completo.setForeground(new Color(0,0,255));
        }
        else{
            completo.setText("Todavia quedan libros adeudados en referencia a este remito.");
            completo.setForeground(new Color(255,0,0));
        }
        total.setText(String.valueOf(remito.getTotal()));
        this.setTitle("Remito nro "  + remito.getNumero() + " de " + remito.getCliente().getNombre());
            
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        completo = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        numero = new javax.swing.JLabel();
        fecha = new javax.swing.JLabel();
        cliente = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        total = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        jLabel1.setText("Numero: ");

        jLabel2.setText("Fecha: ");

        jLabel3.setText("Cliente:");

        completo.setText("jLabel4");

        jTable1.setModel(getModel());
        jScrollPane1.setViewportView(jTable1);

        numero.setText("jLabel4");

        fecha.setText("jLabel5");

        cliente.setText("jLabel6");

        jButton1.setText("Cerrar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel7.setText("Precio Total:");

        total.setText("jLabel8");

        jLabel9.setText("$");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 494, Short.MAX_VALUE)
                            .addComponent(jLabel3)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(numero, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(fecha, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(cliente, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)
                                    .addComponent(completo, javax.swing.GroupLayout.PREFERRED_SIZE, 434, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addGap(20, 20, 20)
                                .addComponent(jLabel9)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(total, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(403, 403, 403)
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(numero))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(fecha))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cliente))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(completo)
                .addGap(15, 15, 15)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(total)
                    .addComponent(jLabel7)
                    .addComponent(jLabel9))
                .addGap(19, 19, 19)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed
    public DefaultTableModel getModel(){
        return this.dtm;
    }
    public void setTotal(){
        this.total.setText(String.valueOf(remito.getTotal()));
    }
    
    public void initTabla(){
        dtm = new DefaultTableModel();
        dtm.addColumn("Cod");
        dtm.addColumn("Titulo");
        dtm.addColumn("Precio");
        dtm.addColumn("Cant.");
        dtm.addColumn("Subtotal");
        
        LinkedList detalles = remito.getDetalles();
        for(int i = 0; i < detalles.size(); i++){
            DetalleRemito dr = (DetalleRemito) detalles.get(i);
            dtm.addRow(getFila(dr));
        }
        detalles.clear();        
    }
    
    private Object[] getFila(DetalleRemito dr){
        Object[] datos;
        datos = new Object[5];
        datos[0] = dr.getEjemplar().getLibro().getId();
        datos[1] = dr.getEjemplar().getLibro().getTitulo();
        datos[2] = dr.getEjemplar().getPrecioUnitario();
        datos[3] = dr.getCantidad(); 
        datos[4] = (dr.getEjemplar().getPrecioUnitario() * dr.getCantidad());
        
        return datos;
    }
    public void insertarFila(DetalleRemito dr){
        this.dtm.addRow(getFila(dr));
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel cliente;
    private javax.swing.JLabel completo;
    private javax.swing.JLabel fecha;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel numero;
    private javax.swing.JLabel total;
    // End of variables declaration//GEN-END:variables
    private DefaultTableModel dtm;
}
